/**
 * @author casazaft
 */
Package("xclab.utils").Interface("ILogger", {
    debug: function(){
    },
    info: function(){
    },
    warn: function(){
    },
    error: function(){
    },
    fatal: function(){
    }
})


Package("xclab.utils").Class("PopupLogger", {
    PopupLogger: function(level){
 this.Super.constructor(level);    
        this.level = level;
        if (!this._window || !this._window.document) {
            this._window = window.open("", 'logger_popup_window', 'width=420,height=320,scrollbars=1,status=0,toolbars=0,resizable=1');
            if (!this._window) {
                obj.popupBlocker = true;
                alert("You have a popup window manager blocking the log4js log popup display.\n\nThis must be disabled to properly see logged events.");
                return;
            }
            if (!this._window.document.getElementById('loggerTable')) {
                this._window.document.writeln("<table width='100%' id='loggerTable'><tr><th align='left'>Time</th><th width='100%' colspan='2' align='left'>Message</th></tr></table>");
                this._window.document.close();
            }
        }
        this.tbl = this._window.document.getElementById("loggerTable");
        
    },
    Statics: {
        DEBUG: 1,
        INFO: 2,
        WARN: 3,
        ERROR: 4,
        FATAL: 5,
        NONE: 6,
        DEBUG_COLOR: "green",
        INFO_COLOR: "blue",
        WARN_COLOR: "yellow",
        ERROR_COLOR: "red",
        FATAL_COLOR: "red"
    },
    Prototype: {
        setLevel: function(level){
            this.level = level;
        },
        getLevel: function(){
            return this.level;
        },
        debug: function(s){
            var Log = this.getClass();
            
            if (this.getLevel() <= Log.DEBUG) {
                this._log(s, "DEBUG");
            }
        },
        info: function(s){
            var Log = this.getClass();
            if (this.getLevel() <= Log.INFO) {
                this._log(s, "INFO");
            }
        },
        warn: function(s){
            var Log = this.getClass();
            if (this.getLevel() <= Log.WARN) {
                this._log(s, "WARN");
            }
        },
        error: function(s){
            var Log = this.getClass();
            if (this.getLevel() <= Log.ERROR) {
                this._log(s, "ERROR");
            }
        },
        fatal: function(s){
            var Log = this.getClass();
            if (this.getLevel() <= Log.FATAL) {
                this._log(s, "FATAL");
            }
        },
        _log: function(msg, level, color){
            if (!color) {
                var thisClass = this.getClass();
                color = thisClass[level + "_COLOR"];
            }
            var tbl = this.tbl;
            var row = tbl.insertRow(-1);
            
            var cell_1 = row.insertCell(-1);
            var cell_2 = row.insertCell(-1);
            var cell_3 = row.insertCell(-1);
            
            var d = new Date();
            var h = d.getHours();
            if (h < 10) {
                h = "0" + h;
            }
            var m = d.getMinutes();
            if (m < 10) {
                m = "0" + m;
            }
            var s = d.getSeconds();
            if (s < 10) {
                s = "0" + s;
            }
            var date = (d.getMonth() + 1) + "/" + d.getDate() + "/" + d.getFullYear() + "&nbsp;-&nbsp;" + h + ":" + m + ":" + s;
            
            cell_1.style.fontSize = "8pt";
            cell_1.style.fontWeight = "bold";
            cell_1.style.paddingRight = "6px";
            
            cell_2.style.fontSize = "8pt";
            cell_2.style.color = color;
            
            
            cell_3.style.fontSize = "8pt";
            cell_3.style.whiteSpace = "nowrap";
            cell_3.style.width = "100%";
            
            if (tbl.rows.length % 2 == 0) {
                cell_1.style.backgroundColor = "#eeeeee";
                cell_2.style.backgroundColor = "#eeeeee";
                cell_3.style.backgroundColor = "#eeeeee";
            }
            
            cell_1.innerHTML = date
            cell_2.innerHTML = level;
            cell_3.innerHTML = msg;
        },
        clear: function(){
            this.tbl.innerHTML = "<tr><th align='left'>Time</th><th width='100%' colspan='2' align='left'>Message</th></tr>";
        }
    }
});


Package("xclab.utils").Class("LoggerFactory", {
    LoggerFactory: function(){
 this.Super.constructor();    
    },
    Prototype: {
        getInstance: function(name, level){
            switch (name) {
                case 'PopupLogger':{
                    return new xclab.utils.PopupLogger(level);
                    
                }
            }
        },
        DEBUG: 1,
        INFO: 2,
        WARN: 3,
        ERROR: 4,
        FATAL: 5,
        NONE: 6
    },
    Extends: j2j.patterns.Singleton
});
var factory = xclab.utils.LoggerFactory.getInstance();
window.log = factory.getInstance('PopupLogger', factory.DEBUG);

